package com.gpoint.rdp.card.department.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.gpoint.rdp.base.core.util.string.StringUtil;
import com.gpoint.rdp.base.web.controller.GenericController;
import com.gpoint.rdp.card.department.service.DepartmentQueryService;
import com.gpoint.rdp.card.department.service.DepartmentService;
import com.gpoint.rdp.common.DoorConfig;
import com.gpoint.rdp.door.campusPid.persistence.dao.CampusPidQueryDao;
import com.gpoint.rdp.door.campusPid.persistence.entity.CampusPidPo;
import com.gpoint.rdp.door.campusPid.service.CampusPidQueryService;
import com.gpoint.rdp.door.user.persistence.entity.CampusCardPo;
import com.gpoint.rdp.door.user.service.CampusCardQueryService;
import com.alibaba.fastjson.JSONObject;
import com.gpoint.rdp.base.core.util.AppUtil;




/**
 * 部门 控制类
 *
 *<pre> 
 * 开发公司：苏州东大金点信息技术有限公司
 * 开发人员：唐红锋
 * 邮箱地址：55872655@qq.com
 * 创建时间：2019-09-10 16:05:40
 *</pre>
 */
@Controller
@RequestMapping("/rdp/popup/")
public class DepartmentPopupController extends GenericController{
	@Resource
	private DepartmentService departmentService;
	@Resource
	private DepartmentQueryService departmentQueryService;
	
	
	@Resource
	private CampusCardQueryService campusCardQueryService;
	
	 
	@Resource
	private CampusPidQueryService campusPidQueryService;
	
	
	
	
	
	
	
	
	
	

	
	
	
	private DoorConfig config = AppUtil.getBean(DoorConfig.class);
	
	String servicecode = config.getServicecode();

	/**
	  *获取列表信息
	  * @param request
	  * @param response
	  * @return
	  * @throws Exception
	  */
	 @RequestMapping("tree")
	 public ModelAndView list(HttpServletRequest request,HttpServletResponse response) throws Exception{
		  ModelAndView mv= new ModelAndView("/rdp/popup/departmentTree.jsp");
		  return mv;	  
	 }
	 
	 
	/**
	  *获取列表信息
	  * @param request
	  * @param response
	  * @return
	  * @throws Exception
	  */
	 @RequestMapping("treeDetail")
	 public ModelAndView treeDetail(@RequestParam String orgcode,HttpServletRequest request,HttpServletResponse response) throws Exception{
		  ModelAndView mv= new ModelAndView("/rdp/popup/memberList.jsp");
		  if(StringUtil.isNotBlank(orgcode)) {
			  mv.addObject("orgcode", orgcode);
		  }else {
			  mv.addObject("orgcode", "");
		  }		 
		  return mv;	  
	 }
	 
	 
		@RequestMapping("add")
		public ModelAndView add(HttpServletRequest request, HttpServletResponse response) throws Exception {
			ModelAndView mv = new ModelAndView("/rdp/popup/meetingManageAdd.jsp");
			return mv;
		}
		
		
		@RequestMapping("downloadExcel")
		public void downloadExcel(HttpServletRequest request, HttpServletResponse response){
			String fileName = "人员导入模板.xls";// 设置文件名，根据业务需要替换成要下载的文件名
			// 设置文件路径
			String realPath = "/temp/";
			byte[] buffer = new byte[1024];
			FileInputStream fis = null;
			BufferedInputStream bis = null;
			try {
				String newpath = this.getClass().getResource(realPath).getPath();
				File file = new File(newpath, fileName);
				fis = new FileInputStream(file);
				bis = new BufferedInputStream(fis);
				fileName = new String(fileName.getBytes(), "ISO-8859-1");//浏览器编码，防止乱码
				response.setContentType("application/force-download");// 设置强制下载不打开
				response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
				OutputStream os = response.getOutputStream();
				int i = bis.read(buffer);
				while (i != -1) {
					os.write(buffer, 0, i);
					i = bis.read(buffer);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (bis != null) {
					try {
						bis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (fis != null) {
					try {
						fis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}

		}

		
		@RequestMapping("downloadIDExcel")
		public void downloadIDExcel(HttpServletRequest request, HttpServletResponse response){
			String fileName = "身份导入模板.xls";// 设置文件名，根据业务需要替换成要下载的文件名
			// 设置文件路径
			String realPath = "/temp/";
			byte[] buffer = new byte[1024];
			FileInputStream fis = null;
			BufferedInputStream bis = null;
			try {
				String newpath = this.getClass().getResource(realPath).getPath();
				File file = new File(newpath, fileName);
				fis = new FileInputStream(file);
				bis = new BufferedInputStream(fis);
				fileName = new String(fileName.getBytes(), "ISO-8859-1");//浏览器编码，防止乱码
				response.setContentType("application/force-download");// 设置强制下载不打开
				response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
				OutputStream os = response.getOutputStream();
				int i = bis.read(buffer);
				while (i != -1) {
					os.write(buffer, 0, i);
					i = bis.read(buffer);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (bis != null) {
					try {
						bis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (fis != null) {
					try {
						fis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}

		}
		
		
		@PostMapping(value = "/uploadExcel")
		@ResponseBody
		public JSONObject uploadExcel(@RequestParam(value = "file", required = false) MultipartFile file) {

			List<String> ids = new ArrayList<String>();
			JSONObject json = new JSONObject();
			json.put("status", true); // 成功
			try {
				// 如果是xls，使用HSSFWorkbook；2003年的excel 如果是xlsx，使用XSSFWorkbook 2007年excel
				HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
				// 根据名称获得指定Sheet对象
				HSSFSheet hssfSheet = workbook.getSheetAt(0);
				for (Row row : hssfSheet) {
					String usernum = null;
					int rowNum = row.getRowNum();
					if (rowNum < 2) {
						continue;
					}
					if (row.getCell(1) != null) {
						row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
						usernum=row.getCell(1).getStringCellValue().trim();
					}
					CampusCardPo po = campusCardQueryService.getByUserNum(usernum);
					if (po != null) {
						ids.add(usernum);
					}
				}
			} catch (Exception e) {
				json.put("status", false);// 失敗
			}
			json.put("ids", ids);
			return json;
		}
	 	
		
		@PostMapping(value = "/uploadIDExcel")
		@ResponseBody
		public JSONObject uploadIDExcel(@RequestParam(value = "file", required = false) MultipartFile file) {

			List<String> ids = new ArrayList<String>();
			JSONObject json = new JSONObject();
			json.put("status", true); // 成功
			try {
				// 如果是xls，使用HSSFWorkbook；2003年的excel 如果是xlsx，使用XSSFWorkbook 2007年excel
				HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
				// 根据名称获得指定Sheet对象
				HSSFSheet hssfSheet = workbook.getSheetAt(0);
				for (Row row : hssfSheet) {
					String pid = null;
					int rowNum = row.getRowNum();
					if (rowNum < 2) {
						continue;
					}
					if (row.getCell(1) != null) {
						row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
						pid=row.getCell(1).getStringCellValue().trim();
					}
					//CampusCardPo po = campusCardQueryService.getByUserNum(usernum); 
					//CampusCardPo po = campusPidQueryService.get
					CampusPidPo po=    campusPidQueryService.getByPid(pid);
//					CampusPidPo po = campusPidQueryDao.getByKey("getByPid", pid);
					if (po != null) {
						ids.add(pid);
					}
				}
			} catch (Exception e) {
				json.put("status", false);// 失敗
			}
			json.put("ids", ids);
			return json;
		}
}